# Read in CA county outlines
ca_counties <- read_sf(here("ca_counties","CA_Counties_TIGER2016.shp"))

# Only keep required attributes
ca_subset <- ca_counties %>% 
  select(NAME, ALAND) %>% 
  rename(county_name = NAME, land_area = ALAND)

# Checked CRS in console
# Read in oil spill data
oil_spill <- read_sf(here("Oil_Spill_Incident_Tracking","Oil_Spill_Incident_Tracking_%5Bds394%5D.shp"))

# Checked CRS in console

Interactive tmap showing oil spill incidents in California

# Make exploratory tmap

# Set viewing mode to interactive
tmap_mode(mode = "view")

tm_shape(ca_subset) +
  tm_fill("land_area", palette = "Blues") +
  tm_shape(oil_spill) +
  tm_dots()

Chloropeth showing the counts of oil spill incidents by county

# Join datasets
ca_oil_spill <- ca_subset %>% 
  st_join(oil_spill)

# Find the counts
oil_spill_counts <- ca_oil_spill %>% 
  count(county_name)

# Make chloropeth map
ggplot(data = oil_spill_counts) +
  geom_sf(aes(fill = n), color = "white", size = 0.1) +
  scale_fill_gradientn(colors = c("lightyellow", "orange", "red")) +
  theme_minimal() +
  labs(fill = "Number of Oil Spill Incidents")